#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2022/9/6 9:09
@File: gui.py
"""
import tkinter as tk
from threading import Thread
from tkinter import filedialog
from tkinter.font import Font
from tkinter.ttk import Combobox
from Common.BladeUploadCase.basic import BladeUpload
import json

'''
离职交接:
前端代码,用tk编写的
'''

if __name__ == '__main__':
    with open('用户配置.json','r',encoding='utf-8') as f:
        BladeUpload.USER=json.load(f)
    windows=tk.Tk()
    windows.title('BLADE用例上传')
    windows.geometry('270x205')
    windows.resizable(width=False, height=False)
    font=Font(family='Consolas',size=10)
    # 第一行:模式 nk4.0 or nk5.0
    tk.Label(windows,text='模式:',font=font).grid(row=0,column=0)
    mode=tk.StringVar()
    mode.set('nk4.0')
    Combobox(windows,state='readonly',font=font,textvariable=mode,values=['nk4.0','nk5.0']).grid(row=0,column=1)
    # 第二行:用户
    tk.Label(windows,text='用户:',font=font).grid(row=1,column=0)
    user=tk.StringVar()
    user.set('')
    Combobox(windows,state='readonly',font=font,textvariable=user,values=[i for i in BladeUpload.USER['nk4.0']]).grid(row=1,column=1)
    # 第三行:路径
    tk.Label(windows,text='路径:',font=font).grid(row=2,column=0)
    path=tk.StringVar()
    path.set('')
    tk.Entry(windows,textvariable=path,font=font).grid(row=2,column=1)
    tk.Button(windows,text='选择',command=lambda :path.set(filedialog.askopenfilename(filetypes=[('数据表',['.xls','.xlsx'])])),font=font).grid(row=2,column=2)
    # 第四行:案例路径
    tk.Label(windows,text='案例路径:',font=font).grid(row=3,column=0)
    _nodePath=tk.StringVar()
    _nodePath.set('/系统用例/01各业务测试用例/投资业务/STM/指标/')
    tk.Entry(windows,textvariable=_nodePath,font=font).grid(row=3,column=1)
    # 第五行:案例名
    tk.Label(windows,text='案例名:',font=font).grid(row=4,column=0)
    caseName=tk.StringVar()
    caseName.set('场景1')
    tk.Entry(windows,textvariable=caseName,state='normal').grid(row=4,column=1)
    # 第六行:案例类型
    tk.Label(windows,text='案例类型:',font=font).grid(row=5,column=0)
    caseType=tk.StringVar()
    caseType.set('1-手工')
    Combobox(windows,state='normal',font=font,textvariable=caseType,values=['1-手工','2-自动化']).grid(row=5,column=1)
    # 第七行:优先级
    tk.Label(windows,text='优先级:',font=font).grid(row=6,column=0)
    casePrior=tk.StringVar()
    casePrior.set('P2')
    Combobox(windows,state='readonly',font=font,textvariable=casePrior,values=['P0','P1','P2','P3']).grid(row=6,column=1)
    # 第八行:操作按钮
    log=tk.StringVar()
    log.set('')
    def upload():
        log.set('running')
        try:
            data={
                '_nodePath': _nodePath.get(),  # 路径
                "caseName": caseName.get(),  # 案例名
                "caseType": caseType.get().split('-')[0],  # 案例类型 1-手工,2-自动化
                "casePrior": casePrior.get(),  # 优先级P0-3
                "caseSideType": "0",  # 正例 or 反例
                "caseDetailType": "0",  # 用例类型 0-功能,1-性能,2-文档,3-安全,4-兼容性,5-可靠性,6-用户体验,7-安装部署
                "caseLayer": "1",  # 用例分层 0-系统级,1-模块级
            }
            BladeUpload(mode.get(),user.get()).run_excel(path.get(),**data)
        except Exception as e:
            with open('./log.txt', 'a+') as f:
                f.write(str(e) + '\n')
        finally:
            log.set('done')
    tk.Button(windows,text='上传',command=lambda:Thread(target=upload).start()).grid(row=8,column=0,columnspan=3)
    # 第九行:简单日志
    tk.Label(windows,textvariable=log,font=font).grid(row=7,column=0,columnspan=3)
    windows.mainloop()
